/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yucc.girlimages.ui;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.AdapterView;

import com.yucc.girlimages.R;
import com.yucc.girlimages.io.StreamLoader;
import com.yucc.girlimages.io.upyun.FolderItem;

/**
 * The main fragment that powers the ImageGridActivity screen. Fairly straight
 * forward GridView implementation with the key addition being the ImageWorker
 * class w/ImageCache to load children asynchronously, keeping the UI nice and
 * smooth and caching thumbnails for quick retrieval. The cache is retained over
 * configuration changes like orientation change so the images are populated
 * quickly as the user rotates the device.
 */
public class CategoryThumbsFragment extends BaseThumbsFragment implements
		 LoaderCallbacks<ArrayList<FolderItem>> {
	protected static String TAG = "ImageGridFragment";

	protected static final String EXTRA_DIRECTORY = "extra_directory";
	protected static final String EXTRA_IS_CATEGORY_REQUEST = "extra_is_category_request";

	private static final int STREAM_LOADER_ID = 0;
	private String mCategory;
	private String mImageDir;
	// 需要区分,获取的是缩略图目录还是文件目录
	private boolean isCategoryRequest;
	private StreamLoader mStreamLoader ;
	private int lastSavedFirstVisibleItem = -1;

	public static final CategoryThumbsFragment newInstance(Bundle args) {
		final CategoryThumbsFragment fragment = new CategoryThumbsFragment();
		fragment.setArguments(args);
		return fragment;
	}

	public static final CategoryThumbsFragment newInstance(String category,
			boolean isCategoryRequest) {
		Bundle args = new Bundle();
		args.putBoolean(EXTRA_IS_CATEGORY_REQUEST, isCategoryRequest);
		args.putString(EXTRA_CATEGORY, category);
		return newInstance(args);
	}

	/**
	 * Empty constructor as per the Fragment documentation
	 */
	public CategoryThumbsFragment() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);

		if(getArguments() != null){
			mCategory = getArguments().getString(EXTRA_CATEGORY);
		}

		isCategoryRequest = getArguments().getBoolean(
				EXTRA_IS_CATEGORY_REQUEST, false);
		
		//根据传递的类别得到对应的标题
		try {
			getSherlockActivity().getSupportActionBar().setTitle(R.string.class.getDeclaredField("text_"+mCategory).getInt(null)) ;
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (isCategoryRequest)
			mImageDir = mCategory + "/thumbs";
		else
			mImageDir = mCategory;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		getLoaderManager().initLoader(STREAM_LOADER_ID, null, this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		final Intent i = new Intent(getActivity(), DetailThumbsActivity.class);
		i.putExtra(DetailThumbsFragment.EXTRA_CATEGORY ,mCategory);
		i.putExtra(DetailThumbsFragment.EXTRA_ITEM_NUMBER, (int)id + 1) ;
		i.putExtra(DetailThumbsFragment.EXTRA_TOAST_ENABLED, true) ;
		startActivity(i);
//		if (getFragmentManager()
//				.findFragmentByTag(TAG) == null) {
//			
//			final FragmentTransaction ft = getFragmentManager()
//					.beginTransaction();
//			ft.replace(android.R.id.content,
//					ImageGridDetailFragment.newInstance(mCategory,(int)id + 1,true) , TAG);
//			if(ft.isAddToBackStackAllowed()) ft.addToBackStack(null);
//			ft.commit();
//		}
	}

	@Override
	public Loader<ArrayList<FolderItem>> onCreateLoader(int arg0, Bundle arg1) {
		mStreamLoader = new StreamLoader(getSherlockActivity(), mImageDir).setCategoryRequest(true) ;
		return mStreamLoader;
	}

	@Override
	public void onLoadFinished(Loader<ArrayList<FolderItem>> arg0,
			ArrayList<FolderItem> list) {
		if (list != null) {
			getExtImageWorkerAdapter().setList(list);
		}
		getImageAdapter().notifyDataSetChanged();
	}

	@Override
	public void onLoaderReset(Loader<ArrayList<FolderItem>> arg0) {

	}
}
